﻿using netDxf;
using netDxf.Entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TryReadDxfs
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string d = Directory.GetDirectories(Directory.GetCurrentDirectory()).FirstOrDefault();
            if (d != null)
            {
                Console.WriteLine(d);
                foreach (string dxfFile in Directory.GetFiles(d, "*.dxf", SearchOption.AllDirectories))
                {
                    Console.WriteLine(dxfFile);
                    string msg = "";
                    bool canRead = false;
                    try
                    {

                        DxfDocument doc = DxfDocument.Load(dxfFile);
                        if (doc.Layouts.SelectMany(layout => doc.Layouts.GetReferences(layout).OfType<EntityObject>()).Any())
                            canRead = true;
                        else
                        {
                            canRead = false;
                            msg = "无法检测到任何图像。";
                        }
                    }
                    catch (Exception ex)
                    {
                        msg = ex.Message;
                    }
                    if (canRead)
                    {
                        File.Delete(dxfFile);
                    }
                    else
                    {
                        Console.WriteLine($"{dxfFile.Replace(d, "")}:{msg}");
                    }
                }
            }

            Console.WriteLine("程序结束。");
            Console.ReadKey();
        }
    }
}
